package com.itany.book.conf;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;

//@Configuration
public class CasClientConfig {

    //cas-server的前缀
    private static final  String CAS_SERVER_URL_PREFIX="http://localhost:8443/cas/";
    //cas-server的登陆界面
    private static final String CAS_SERVER_URL_LOGIN="http://localhost:8443/cas/login";
    //本机名称
    private static   final String SERVER_NAME="http://localhost:9005/";

    /**
     * 配置登出界面
     * @return
     */
    @Bean
    public FilterRegistrationBean filterSingleRegistration(){
        System.out.println("===============filterSingleRegistration===================");
        FilterRegistrationBean  bean = new FilterRegistrationBean();
        bean.setFilter(new SingleSignOutFilter());
        // 拦截所有请求，因为登出方法会重定向到cas logout服务
        // cas 服务会发送到所有的项目中，清楚相关的用户信息。
        bean.addUrlPatterns("/book/*");
        Map<String,String> map = new HashMap<String,String>();
        map.put("casServerUrlPrefix",CAS_SERVER_URL_PREFIX);
        bean.setInitParameters(map);
        bean.setOrder(1);
        return bean;
    }

    @Bean
   public FilterRegistrationBean  filterValidationRegistration(){
       System.out.println("===============filterValidationRegistration===================");
       FilterRegistrationBean  bean = new FilterRegistrationBean();
       bean.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
       //设置匹配路径
       Map<String,String> map = new HashMap<String,String>();
       // 配置代理服务器
       bean.addUrlPatterns("/book/*");
       map.put("casServerUrlPrefix",CAS_SERVER_URL_PREFIX);
       map.put("serverName",SERVER_NAME);
       map.put("useSession","true");
       bean.setInitParameters(map);
       bean.setOrder(1);
       return bean;
   }

    /**
     * 配置登陆
     * @return
     */
    @Bean
    public FilterRegistrationBean filterAuthenRegistration(){
        System.out.println("===============filterAuthenRegistration===================");
        FilterRegistrationBean  bean = new FilterRegistrationBean();
        bean.setFilter(new AuthenticationFilter());
        //设置匹配路径
        Map<String,String> map = new HashMap<String,String>();
        // 对于所有需要登陆才能使用的页面，需要配置url路径，
        bean.addUrlPatterns("/book/*");
        map.put("casServerLoginUrl",CAS_SERVER_URL_LOGIN);
        map.put("serverName",SERVER_NAME);

        // 是不是放置多个pattern即可？
        // map.put("ignorePattern","/user/userServer/casLogOut");
        // map.put("ignoreUrlPatternType","com.itany.book.conf.SimplerUrlPatterMatcherStrategy");
        bean.setInitParameters(map);
        bean.setOrder(1);
        return bean;

    }

    @Bean
    public FilterRegistrationBean filterWapperRegistration(){
        System.out.println("===============filterWapperRegistration===================");
        FilterRegistrationBean  bean = new FilterRegistrationBean();
        bean.setFilter(new HttpServletRequestWrapperFilter());
        Map<String,String> map = new HashMap<String,String>();
        // 包装过滤器，可以额外获得除username之外的放在cas服务器之外的信息。
        bean.addUrlPatterns("/book/*");
        bean.setInitParameters(map);
        bean.setOrder(1);
        return bean;
    }

    @Bean
    // 登出监听器，当cas登出时，需要向所有的项目发送登出，消除session里的缓存
    public ServletListenerRegistrationBean<EventListener> logOutListener(){
        System.out.println("===============logOutListener===================");
        ServletListenerRegistrationBean<EventListener> listener=new ServletListenerRegistrationBean<EventListener>();
        listener.setListener(new SingleSignOutHttpSessionListener());
        listener.setOrder(1);
        return listener;
    }
}
